const request = require('request');

let options = {
    method : 'GET',
    url : "https://api.imjad.cn/cloudmusic/",
    qs: { type: 'lyric', id: '' },
    headers:
        {
            'Cache-Control': 'no-cache' 
        }
}
let json = {};

    let getLyrics = (response, id)=>{
    options.qs.id = id;
    request(options,function(err, res, body){
      if(err){
          throw new Error(err)
      }
      else{
       
          json = body;
          let buf = new Buffer.from(json);
          json = JSON.parse(buf);
   
          let lyric = json.lrc.lyric
         //算法算法 将歌词的格式化为[{lyric:123,time：12.03}]
          let lyrics1 = [];
          let songsData = [];//存储单位
           lyrics1 =  lyric.split('[');
      
           lyrics1.forEach(function(index, item){
           if(index){
            let data = index.split(']');
            let dataJson = {time:data[0], lyric: data[1]};
           if(data[1] != '\n'){
           songsData.push(dataJson);
        }
              }
           })
       
          response.send(songsData);
        
      }
    })

}
    module.exports = getLyrics;